package org.graduates.webapp.actions.post;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.graduates.webapp.actions.BaseAction;
import org.projects.graduates.domain.Person;
import org.projects.graduates.domain.Post;
import org.projects.graduates.domain.PostReply;
import org.projects.graduates.domain.Reply;

@ParentPackage("web")
@Action(results = { @Result(name = "success", type = "json", 
params={"includeProperties","pid,reply, reply.id, reply.content,reply.upDate, reply.who, reply.who.id, reply.who.showName," +
		"cPerson, cPerson.id, cPerson.showName"}) })
public class CommitReplyAction extends BaseAction {

	private static final long serialVersionUID = 2845142619586773163L;

	private Reply reply;

	// post.id
	private long pid = 0L;

	// 当前用户
	private Person cPerson = null;

	public String execute() {
		if (null == reply || 0L == pid) {
			return INPUT;
		}
		
		Post post = Post.get(Post.class, pid);
		
		if (null == post) {
			return INPUT;
		}
		
		PostReply postReply = PostReply.findBy(post);
		
		if (null == postReply) {
			return INPUT;
		}
		
		reply.setWho(getCurrentPerson());
		reply.setUpDate(new Date());
		this.gradApplication.save(reply);
		
		postReply.getReplies().add(reply);
		this.gradApplication.save(postReply);
		
		
		
		cPerson = super.getCurrentPerson();
		
		return SUCCESS;
	}

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}

	public long getPid() {
		return pid;
	}

	public void setPid(long pid) {
		this.pid = pid;
	}

	public Person getcPerson() {
		return cPerson;
	}

	public void setcPerson(Person cPerson) {
		this.cPerson = cPerson;
	}

}
